﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Frm
{
    public partial class FrmCompareFile : Form
    {
        public FrmCompareFile()
        {
            try
            {
                InitializeComponent();
                this.Icon = YTSK.Properties.Resources.Fly;
                this.dDate.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                var sksetsoft = cSK.cSKSet.loadsave(SkConfig.税控软件名称);
                if (sksetsoft.ObjToString() == "")
                { sksetsoft = "航天"; }
                var sksoft = (SKSoftName)Enum.Parse(typeof(SKSoftName), sksetsoft);
                switch (sksoft)
                {
                    case SKSoftName.航天:
                    case SKSoftName.航天新版:
                        this.cSKBillType.Items.Add("专普发票数据");
                        this.cSKBillType.Items.Add("电子普通发票");
                        break;
                    default:
                        this.cSKBillType.Items.Add("专用发票");
                        this.cSKBillType.Items.Add("普通发票");
                        break;
                }
                this.cSKBillType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }

        private void btOpen_Click(object sender, EventArgs e)
        {
            var filename = this.tbXlsFileName.Text.Trim();
            if (System.IO.File.Exists(filename))
            {
                System.Diagnostics.Process.Start(filename);
            }
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            var dd = this.dDate.Value.ToString("yyyy-MM-dd");
            var sk = this.cSKBillType.Text;
            var filename = this.tbXlsFileName.Text.Trim();
            if (!System.IO.File.Exists(filename))
            {
                MessageBox.Show("文件没有找到");
                return;
            }
            var tip = new FrmTip();
            tip.setValue("正在上传文件，请耐心等待", 20);
            tip.Show();
            Application.DoEvents();
            try
            {
                var sksetsoft = cSK.cSKSet.loadsave(SkConfig.税控软件名称);
                if (sksetsoft.ObjToString() == "")
                { sksetsoft = "航天"; }
                var sksoft = (SKSoftName)Enum.Parse(typeof(SKSoftName), sksetsoft);
                string url;
                switch (sksoft)
                {
                    case SKSoftName.百望:
                        url = string.Format("{0}/api/oms/SKCompare/UpSKExcel_BW?token={1}&dDate={2}&cSKBillType={3}&uploadfile=1",
                        ServerConfig.ServerAddress, ServerConfig.GetToken(), dd, sk);
                        break;
                    case SKSoftName.航天:
                    default:
                        if (ServerConfig._company == Company.上海Iteng)
                        {
                            url = string.Format("{0}/api/oms/YTSH_SKCompare/UpSKExcel_HT?token={1}&dDate={2}&cSKBillType={3}&uploadfile=1",
                       ServerConfig.ServerAddress, ServerConfig.GetToken(), dd, sk);
                        }
                        else
                        {
                            url = string.Format("{0}/api/oms/SKCompare/UpSKExcel_HT?token={1}&dDate={2}&cSKBillType={3}&uploadfile=1",
                          ServerConfig.ServerAddress, ServerConfig.GetToken(), dd, sk);
                        }
                        break;
                }
                var s = Common.handleHttp.FileUpload(filename, "", url);
                tip.setValue("正在上传文件，请耐心等待", 80);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show(result.resultdetail, "部分发票数据与税控对账有错"); return; }
                else { MessageBox.Show(string.Format("恭喜恭喜，[{0}]数据校对完全正确。", sk)); this.Close(); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally { tip.Close(); }
        }

        private void btDir_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
            of.Multiselect = false;
            of.ShowDialog();
            tbXlsFileName.Text = of.FileName;
        }

        private void tbXlsFileName_TextChanged(object sender, EventArgs e)
        {
            var filename = this.tbXlsFileName.Text;
            var sksetsoft = cSK.cSKSet.loadsave(SkConfig.税控软件名称);
            if (sksetsoft.ObjToString() == "")
            { sksetsoft = "航天"; }
            var sksoft = (SKSoftName)Enum.Parse(typeof(SKSoftName), sksetsoft);
            if (sksoft == SKSoftName.航天)
            {
                if (filename.Contains("专普发票")) { this.cSKBillType.SelectedIndex = 0; }
                if (filename.Contains("电子普通发票")) { this.cSKBillType.SelectedIndex = 1; }
            }

        }
    }
}
